<?php
include_once('./modules/head.php');
require_once('./db/pdo.php');
require_once('./users/users.php');
require_once('./sanitizer.php');
require_once('./validate.php');

$fields = [
    'name' => 'string',
    'email' => 'email',
    'username' => 'string',
    'phone' => 'string',
    'website' => 'url',
];

$errors = [
    'email' => '',
    'phone' => '',
    'website' => ''
];

if ($_SERVER['REQUEST_METHOD'] === 'POST')
{
    $user = sanitize($_POST, $fields);
    $errors = validateUserInfo($user);
    if ((empty($errors['email']) && empty($errors['phone']) && empty($errors['website'])) == TRUE)
    {
        createUser($user, $dbh);
        echo "<p style='color: red; text-align: center;'>User was created</p>";
    }
}
?>
<div class="container">
    <form class="row g-1" method="post" enctype="multipart/form-data">
        <div class="mb-3">
            <label for="inputEmail" class="form-label">Email address</label>
            <input type="email" class="form-control" id="inputEmail" name="email">
            <p><?= $errors['email'] ?></p>
        </div>
        <div class="mb-3">
            <label for="inputPassword" class="form-label">Phone</label>
            <input type="phone" class="form-control" id="inputPassword" name="phone">
            <p><?= $errors['phone'] ?></p>
        </div>
        <div class="mb-3">
            <label for="exampleInputEmail1" class="form-label">Name</label>
            <input type="text" class="form-control" id="exampleInputEmail1" name="name">
        </div>
        <div class="mb-3">
            <label for="exampleInputEmail1" class="form-label">Username</label>
            <input type="text" class="form-control" id="exampleInputEmail1" name="username">
        </div>
        <div class="mb-3">
            <label for="exampleInputEmail1" class="form-label">Website</label>
            <input type="text" class="form-control" id="exampleInputEmail1" name="website">
            <p><?= $errors['website'] ?></p>
        </div>
        <div class="col-10">
            <button class="btn btn-primary" type="submit">Submit form</button>
        </div>
        <div class="col-2" style="text-align: right;">
            <a href="./index.php" class="btn btn-danger">Back to users list</a>
        </div>        
    </form>
</div>
